package lazy2.util;

import java.lang.reflect.*;

public class ObjectUtil {
	public static void main(String[] args) {
		System.out.println(returnLong(5));
	}
	public static void main2(String[] args) {
		long ms, avg = 0;
		Object o = new Object();
		ForLoop:
		for(int time=0;time<10;time++) {
			ms = System.currentTimeMillis();
			for(int i=0;i<100000;i++) {
				if(getMethod(o, "wait", Long.TYPE)==null) {
					break ForLoop;
				}
			}
			ms = System.currentTimeMillis()-ms;
			avg += ms;
			System.out.println(ms);
		}
		System.out.println("avg ms:"+avg/10);
	}
	public static long returnLong(int n) {
		Integer i = n;
		System.out.println(Integer.TYPE.isInstance(n));
		return n;
	}
	public static Method getMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) {
		Method method = null;
		if(cls!=null) {
			try {
				method = cls.getMethod(methodName, parameterTypes);
			} catch (Exception e) {
				//e.printStackTrace();
			}
		}
		return method;
	}
	public static Method getMethod(Object target, String methodName, Class<?>... parameterTypes) {
		Method method = null;
		if(target!=null) {
			Class<?> cls;
			if(target instanceof Class<?>) {
				cls = (Class<?>)target;
			} else {
				cls = target.getClass();
			}
			method = getMethod(cls, methodName, parameterTypes);
		}
		return method;
	}
	public static Object invoke(Object target, Method method, Object... args) {
		Object value = null;
		if(target!=null && method!=null) {
			boolean isOkToInvoke = false;
			Class<?>[] parameterTypes = method.getParameterTypes();
			if(parameterTypes!=null) {
				isOkToInvoke = (args!=null && parameterTypes.length==args.length);
			} else {
				isOkToInvoke = (args==null || args.length==0);
			}
			if(isOkToInvoke) {
				try {
					value = method.invoke(target, args);
				} catch (Exception e) {
					//e.printStackTrace();
				}
			}
		}
		return value;
	}
	
	public static void test() throws Exception {
		Object o = new Object();
		Class<?> cls = o.getClass();
		Method method = cls.getMethod("hashCode");
	}
	public static void test2() throws Exception {
		
	}
	public static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object[] args) {
		Object value = null;
		if(target!=null) {
			Class<?> cls = target.getClass();
			Method method = null;
			try {
				method = cls.getMethod(methodName, parameterTypes);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		return value;
	}
}
